package util.ir;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;

import util.io.FileInput;

public class SearchMixLanguageModel {

	private static String fields_trigrams[] = { "type", "a", "b", "c", "f_t",
			"f_12", "f_23", "f_13", "f_t_b", "f_12_b", "f_23_b", "f_13_b" };
	private static String fields_trigrams_simple[] = { "type", "trigram",
			"f_t", "f_12", "f_23", "f_13", "f_t_b", "f_12_b", "f_23_b",
			"f_13_b" };

	private static String fields_bigrams[] = { "type", "a", "b", "f_b", "f_1",
			"f_2", "f_b_b", "f_1_b", "f_2_b" };
	private static String fields_bigrams_simple[] = { "type", "bigram", "f_b",
			"f_1", "f_2", "f_b_b", "f_1_1", "f_2_1" };

	static String index = "";

	public static String createOrQuery(String fields, String seeds[]) {

		String special_seed = "";

		for (int i = 0; i < seeds.length; i++) {

			special_seed = special_seed + fields + ":" + seeds[i] + " ";

		}

		special_seed = "(" + special_seed.trim() + ")";

		return special_seed;
	}

	public static HashSet<String[]> trigramSearch(String index, String t[],
			String fields[]) throws CorruptIndexException, IOException,
			ParseException {

		IndexSearcher searcher = new IndexSearcher(index);

		Analyzer analyzer = new StandardAnalyzer();
		QueryParser queryParser = new QueryParser("<default field>", analyzer);
		// <default field> is the field that QueryParser will search if you
		// don't
		// prefix it with a field.

		String queries[] = new String[fields.length];

		for (int i = 0; i < queries.length; i++) {

			queries[i] = createOrQuery(fields[0], t);

		}

		// Matching

		Hits hits = searcher.search(queryParser.parse(queries[0]));
		HashSet<String[]> response = new HashSet<String[]>();

		for (int i = 0; i < hits.length(); i++) {

			Document doc = hits.doc(i);

			String r[] = new String[fields_trigrams.length];
			String temp = "";
			for (int j = 0; j < fields_trigrams.length; j++) {
				r[j] = doc.get(fields_trigrams[j]);
				temp = temp + r[j] + "\t";

			}

			// System.out.println(temp);

			response.add(r);

		}

		// swap answers here c for b and b for c
		hits = searcher.search(queryParser.parse(queries[1]));

		for (int i = 0; i < hits.length(); i++) {

			Document doc = hits.doc(i);

			String r[] = new String[fields_trigrams.length];
			String temp = "";

			String b_temporal = "";
			for (int j = 0; j < fields_trigrams.length; j++) {

				if (fields_trigrams[j].equals("b")) {
					b_temporal = doc.get(fields_trigrams[j]);
					r[j] = doc.get("c");

				} else if (fields_trigrams[j].equals("c")) {
					r[j] = b_temporal;

				} else {
					r[j] = doc.get(fields_trigrams[j]);

				}

				temp = temp + r[j] + "\t";

			}

			// System.out.println(temp);

			response.add(r);

		}
		System.out.println(response.size());
		searcher.close();
		return response;

	}

	public static HashSet<String[]> trigramSearchSimple(String index,
			String t[], String model) throws CorruptIndexException,
			IOException, ParseException {

		RAMDirectory dir = new RAMDirectory(index);
		IndexSearcher searcher = new IndexSearcher(dir);

		Analyzer analyzer = new WhitespaceAnalyzer();
		QueryParser queryParser = new QueryParser("trigram", analyzer);
		// <default field> is the field that QueryParser will search if you
		// don't
		// prefix it with a field.

		String query = "";

		for (int i = 0; i < t.length; i++) {

			query = query + t[i] + "\t";

		}

		query = query.trim();

		query = query + "  AND (type:" + model+ ")";

		// Matching
		System.out.println(query);
		Hits hits = searcher.search(queryParser.parse(query));
		HashSet<String[]> response = new HashSet<String[]>();

		for (int i = 0; i < hits.length(); i++) {

			Document doc = hits.doc(i);

			String r[] = new String[fields_trigrams_simple.length];
			String temp = "";
			for (int j = 0; j < fields_trigrams_simple.length; j++) {
				r[j] = doc.get(fields_trigrams_simple[j]);
				temp = temp + r[j] + "\t";

			}

			System.out.println(temp);

			response.add(r);

		}

		// swap answers here c for b and b for c
		System.out.println(response.size());
		searcher.close();
		return response;

	}

	public static HashSet<String[]> bigramSearchSimple(String index,
			String t[], String model) throws CorruptIndexException,
			IOException, ParseException {

		RAMDirectory dir = new RAMDirectory(index);
		IndexSearcher searcher = new IndexSearcher(dir);

		Analyzer analyzer = new WhitespaceAnalyzer();
		QueryParser queryParser = new QueryParser("bigram", analyzer);
		// <default field> is the field that QueryParser will search if you
		// don't
		// prefix it with a field.

		String query = "";

		for (int i = 0; i < t.length; i++) {

			query = query + t[i] + "\t";

		}

		query = query.trim();

		query = query + "  AND (type:" + model +  ")";

		// Matching
		System.out.println(query);
		Hits hits = searcher.search(queryParser.parse(query));
		HashSet<String[]> response = new HashSet<String[]>();

		for (int i = 0; i < hits.length(); i++) {

			Document doc = hits.doc(i);

			String r[] = new String[fields_bigrams_simple.length];
			String temp = "";
			for (int j = 0; j < fields_bigrams_simple.length; j++) {
				r[j] = doc.get(fields_bigrams_simple[j]);
				temp = temp + r[j] + "\t";

			}

			System.out.println(temp);

			response.add(r);

		}

		// swap answers here c for b and b for c
		System.out.println(response.size());
		searcher.close();
		return response;

	}

	// static IndexSearcher searcher=null;

	/**
	 * 1 index/query 2 index path 3 query
	 */

	static public void main(String[] args) throws CorruptIndexException,
			LockObtainFailedException, IOException, ParseException {
		// TODO Auto-generated method stub

		String path = "/home/sergio/delicious_index/trigrams_index_mix";

		String fields[] = { "b", "c" };

		String t[] = { "a", "bb", "youtube", "videos", "games", "social",
				"media", "songs" };
		trigramSearchSimple(path, t, "kids");

	}

}
